<?php
namespace App\Logic;

use Validator;
use App\Models\User;

class UserLogic
{
    /**
     * 注册用户
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['password'] 密码
     * @param $param['nickname'] 昵称
     * 
     * @return array
     */
    public function doRegister($param)
    {
        //方便模拟
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';
        $param['password'] = !empty($param['password']) ? $param['password'] : '';
        $param['nickname'] = !empty($param['nickname']) ? $param['nickname'] : '';
        $param['logintype'] = !empty($param['logintype']) ? $param['logintype'] : 1;
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'password.required' => '密码不能为空',
            'nickname.required' => '昵称不能为空',
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'password' => 'required',
            'nickname' => 'required'
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);
        }
        
        $userModel = new User();
        
        //查看用户是否存在
        $userExist = $userModel->userPhoneQuery($param['phone']);
        if ($userExist) {
            return array('code' => 205, 'msg' => '该账号已存在');
        }
        
        //注册
        $register = $userModel->doRegister($param);
        if ($register['userid']) {
            $param['userid'] = $register['userid'];
            
            $loginData = $userModel->login($param);
            if ($loginData) {
                $data['id'] = $register['userid'];
                $data['usertype'] = 0;
                $data['nickname'] = htmlentities($param['nickname']);
                $data['avatar'] = "";
                $data['phone'] = htmlentities($param['phone']);
                $data['messagecount'] = 0;
                $data['createtime'] = $register['createtime'];
                $data['status'] = 1;
                $data['accesstoken'] = $loginData['accesstoken'];
                $data['logintype'] = $loginData['logintype'];
                $data['last_login_time'] = $loginData['last_login_time'];
                
                return array('code' => 200, 'msg' => '注册成功', 'data' => $data);
            } else {
                return array('code' => 200, 'msg' => '注册成功', 'data' => array());
            }
        } else {
            return array('code' => 201, 'msg' => '注册失败');
        }
    }
    
    /**
     * 信息校验
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $errors 错误处理对象
     * @param $msg 错误提示信息
     * 
     * @return array('code'=>$code,//状态码
     *               'msg'=>$msg,//提示语
     *               );
     */
    public function errorMsg($errors, $msg)
    {
        if ($errors->has('phone')) {
            $status = 202;
        } else if ($errors->has('password')) {
            $status = 203;
        } else if($errors->has('accesstoken')) {
            $status = 203;
        } else if($errors->has('nickname')) {
            $status = 204;
        } else if($errors->has('logintype')) {
            $status = 204;
        } else if($errors->has('userid')) {
            $status = 204;
        } else if($errors->has('status')) {
            $status = 207;
        }
        
        return array('code' => $status, 'msg' => $msg);
    }
    
    /**
     * 登录
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['password'] 密码
     * @param $param['logintype'] 登录类型
     * 
     * @return array
     */
    public function login($param)
    {
        //方便模拟
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';
        $param['password'] = !empty($param['password']) ? $param['password'] : '';
        $param['logintype'] = !empty($param['logintype']) ? $param['logintype'] : 1;
        
        //登录信息校验
        $messages = [
            'phone.required' => '账号不能为空',
            'password.required' => '密码不能为空',
            'logintype.required' => '登录类型不能为空',
            'logintype.integer' => '请选择正确的登录类型'
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'password' => 'required',
            'logintype' => 'required|integer'
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            return $this->errorMsg($errors, $msg);
        }
        
        $userModel = new User();
        
        //校验账号密码
        $loginCheck = $userModel->loginCheck($param);
        if ($loginCheck) {
            //登录
            $param['userid'] = $loginCheck->id;
            
            $loginData = $userModel->login($param);
            if ($loginData) {
                $data = $loginCheck;
                $data->nickname = urldecode($data->nickname);
                $data->accesstoken = $loginData['accesstoken'];
                $data->logintype = $loginData['logintype'];
                $data->last_login_time = $loginData['last_login_time'];
                
                return array('code' => 200, 'msg' => '登录成功', 'data' => $data);
            } else {
                return array('code' => 201, 'msg' => '登录失败');
            }
        } else {
            return array('code' => 205, 'msg' => '账号或密码错误');
        }
    }
    
    /**
     * 退出
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * @param $param['logintype'] 登录类型
     * 
     * @return array
     */
    public function logout($param)
    {
        //方便模拟
        $param['phone']= !empty($param['phone']) ? $param['phone'] : '';
        $param['accesstoken']= !empty($param['accesstoken']) ? $param['accesstoken'] : '';
        $param['logintype']= !empty($param['logintype']) ? $param['logintype'] : 1;
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'accesstoken.required' => '操作令牌不能为空',
            'logintype.required' => '登录类型不能为空',
            'logintype.integer' => '请输入正确的登录类型'
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'accesstoken' => 'required',
            'logintype' => 'required|integer'
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);
        }
        
        $userModel = new User();
        
        //操作令牌校验
        $getToken = $userModel->getToken($param);
        if (empty($getToken)) {
            return array('code' => 205, 'msg' => '您还没有登录哟~');
        }
        
        $b = $userModel->logout($param);
        if ($b) {
            return array('code' => 200, 'msg' => '退出成功~');
        } else {
            return array('code' => 201, 'msg' => '操作失败~');
        }
    }
    
    /**
     * 黑名单设置
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * @param $param['userid'] 被操作用户
     * @param $param['status'] 操作行为，0-删除，1-正常，2-拉黑
     * 
     * @return array
     */
    public function blacklistSetting($param)
    {
        //方便模拟
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';
        $param['accesstoken'] = !empty($param['accesstoken']) ? $param['accesstoken'] : '';
        $param['userid'] = !empty($param['userid']) ? $param['userid'] : '';
        $param['status'] = isset($param['status']) ? $param['status'] : 0;
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'accesstoken.required' => '操作令牌不能为空',
            'userid.required' => '请选择操作用户',
            'userid.integer' => '请选择正确的用户',
            'status.required' => '请选择要执行的操作',
            'status.integer' => '请选择正确的操作'
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'accesstoken' => 'required',
            'userid' => 'required|integer',
            'status' => 'required|integer'
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);
        }
        
        $userModel = new User();
        
        //操作令牌校验
        $getToken = $userModel->getToken($param); 
        if (empty($getToken)) {
            return array('code' => 205, 'msg' => '您还没有登录哟~');
        } else if ($getToken->usertype!=1){//不是管理员
            return array('code' => 206, 'msg' => '您没有权限操作~');
        }
        
        //设置黑名单
        $b = $userModel->blacklistSetting($param);
        if ($b) {
            return array('code' => 200, 'msg' => '设置成功~');
        } else {
            return array('code' => 201, 'msg' => '设置失败~');
        }
    }
    
    /**
     * 用户列表
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * @param $param['nickname'] 查询条件，昵称
     * @param $param['startime'] 查询条件，创建开始时间
     * @param $param['endtime'] 查询条件，创建结束时间
     * @param $param['status'] 查询条件，0-全部，1-正常用户，2-黑名单
     * 
     * @return array
     */
    public function userList($param)
    {
        //方便模拟
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';
        $param['accesstoken'] = !empty($param['accesstoken']) ? $param['accesstoken'] : '';
        $param['nickname'] = !empty($param['nickname']) ? $param['nickname'] : '';
        $param['startime'] = !empty($param['startime']) ? $param['startime'] : 0;
        $param['endtime'] = !empty($param['endtime']) ? $param['endtime'] : 0;
        $param['status'] = !empty($param['status']) ? $param['status'] : 0;//0-全部，1-正常用户，2-黑名单
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'accesstoken.required' => '操作令牌不能为空',
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'accesstoken' => 'required',
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);
        }
        
        $userModel = new User();
        
        //操作令牌校验
        $getToken = $userModel->getToken($param);
        if (empty($getToken)) {
            return array('code' => 205, 'msg' => '您还没有登录哟~');
        } else if ($getToken->usertype != 1) {//不是管理员
            return array('code' => 206, 'msg' => '您没有权限操作~');
        }
        
        //获取用户列表
        $userlist = $userModel->userList($param);
        if ($userlist) {
            return array('code' => 200, 'msg' => '用户列表~', 'data' => $userlist);
        } else {
            return array('code' => 201, 'msg' => '请求失败~');
        }
    }
    
    /**
     * 上传
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $request 传输对象
     * 
     * @return array
     */
    public function upload($request)
    {
        //方便模拟
        $param = $request->all();
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';
        $param['accesstoken'] = !empty($param['accesstoken']) ? $param['accesstoken'] : '';
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'accesstoken.required' => '操作令牌不能为空',
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'accesstoken' => 'required',
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);
        }

        $userModel = new User();
        
        //操作令牌校验
        $getToken = $userModel->getToken($param);
        if (empty($getToken)) {
            return array('code' => 205, 'msg' => '您还没有登录哟~');
        }
        
        if ($request->hasFile('photo') && $request->file('photo')->isValid()) {
            $photo = $request->file('photo');
            $extension = $photo->extension();
            $store_result = $photo->store('photo');
            
            $output = [
                'extension' => $extension,
                'store_result' => 'upload/'.$store_result
            ];
            
            return array('code' => 200, 'msg' => '上传成功', 'data' => $output);
        }
        
        return array('code' => 201, 'msg' => '未获取到上传文件或上传过程出错');    
    }
}